/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.client.gui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.atomic.AtomicBoolean;
import pl.cezary.butler.walili.client.LoginControler;

/**
 *
 * @author Czarek
 */
class AppCloseAction extends WindowAdapter {

    private final AtomicBoolean closed = new AtomicBoolean(false);
    private final LoginControler kon;
    private final Runnable custom;    

    public AppCloseAction(LoginControler kon, Runnable custom) {
        this.kon = kon;
        this.custom = custom;
    }

    
    
    public AppCloseAction(final LoginControler kon) {
        this(kon,null);

    }
    

    private void close() {
        if (closed.compareAndSet(false, true)) {
            if(custom!=null)custom.run();
            kon.logout();                        
        }
    }

    @Override
    public void windowClosing(WindowEvent e) {
        super.windowClosing(e);
        close();
    }
}
